package com.essearch.controller;

import com.essearch.document.ProductDocument;
import com.essearch.service.ElasticsearchIndex;
import com.essearch.service.ElasticsearchService;
import com.essearch.service.EsSearchService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.io.IOException;


/**
 * @ Author xsd
 * @ crate time 2020/9/18 15:56
 * @ 描述 索引操作
 */
@RestController
@RequestMapping(value = "index")
public class EsIndexController {
    @Resource
    private EsSearchService esSearchService;
    @Resource // TransportClient 客户端。
    private ElasticsearchService elasticsearchService;
    @Resource
    private ElasticsearchIndex elasticsearchIndex;

    /**删除索引
     * @return TransportClient
     * @throws IOException
     */
    @RequestMapping("del")
    public String recreateIndex(String indexName) throws IOException {
        elasticsearchService.recreateIndex(indexName);
        return "del index ok...";
    }

    /**
     * 创建索引。。
     * @return TransportClient
     * @throws IOException
     */
    @RequestMapping("add")
    public String createIndex(String indexName) throws IOException {
        elasticsearchService.createIndex(indexName,indexName);
        return "create index sucess...";
    }
    /**
     * 删除索引库
     * @param indexName
     * @return ElasticsearchRepository
     */
    @RequestMapping("del/{indexName}")
    public String deleteIndex(@PathVariable String indexName){
        esSearchService.deleteIndex(indexName);
        return "success";
    }
    /**
     * 清空索引
     * @return
     * ElasticsearchRepository
     */
    @RequestMapping("delAll")
    public String deleteAll() {
        esSearchService.deleteAll();
        return "success";
    }
    /**
     * 删除索引
     * @return
     * ElasticsearchRepository
     */
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable String id) {
        esSearchService.delete(id);
        return "success";
    }
    // RestHighLevelClient 客户端。。
    @RequestMapping("exists")
    public boolean delete1() throws Exception{
        boolean isExists=elasticsearchIndex.exists(ProductDocument.class);
        return isExists;
    }
    // RestHighLevelClient 客户端。。
    @RequestMapping("createIndex")
    public String createIndex() throws Exception{
        elasticsearchIndex.createIndex(ProductDocument.class);
        return "ok";
    }
}
